﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace HeavenvsDarkness
{
    public static class Global
    {
       
            public enum EVENT
            {
                GM_NONEEVENT,
                GM_MOUSE_CLICKED,
                GM_MOUSE_RIGHTCLICKED,
                GM_MOUSE_MOVE,
                GM_MONEY_INCREASE,
                GM_MONEY_CHANGED,
                GM_PLAYER_SETPET,
                GM_CURSOR_CLICKEDTOPLACEPET,
                GM_CURSOR_CLICK_TO_CONTROL_PET,
                GM_BUTTON_UPDATESPEED,
                GM_PET_CHOSEN,
                GM_DIALOGFORCE_CHOSEN,
                GM_PLAYER_CHOSENFORCE,
                GM_PET_DIED,
                GM_DROPMONEY,
                GM_LEFTARROW_PRESSED,
                GM_RIGHTARROW_PRESSED,
                GM_UPARROW_PRESSED,
                GM_DOWNARROW_PRESSED,
                GM_ENEMY_CHOSEN,
                GM_PET_MOVE,
                GM_PET_ATTACK,
                GM_BULLET_REACHTARGET,
                GM_PET_REMOVED,
                GM_BULLET_REMOVED,
                GM_CURSOR_CONTROL_PET_MOVE,
                GM_ENTER_CLICKED
            };

            static EVENT gameEvent;

            public static EVENT GameEvent
            {
                get { return gameEvent; }
                set { gameEvent = value; }
            }
            static List<GameEntity> items = new List<GameEntity>();

            public static List<GameEntity> Items
            {
                get { return items; }
                set { items = value; }
            }

            static int indexForm;

            public static int IndexForm
            {
                get { return Global.indexForm; }
                set { Global.indexForm = value; }
            }
            static int indexCursor;

            public static int IndexCursor
            {
                get { return Global.indexCursor; }
                set { Global.indexCursor = value; }
            }
            public static void Update(GameTime gameTime)
            {
                GetCursor().Update(gameTime);
                if (GetForm().IsCanbeSwitch)
                {
                    SetForm();
                    ResetItem();
                }
                else
                {
                    GetForm().Update(gameTime);
                }
            }

            public static void BroastCastEvent(Global.EVENT events, object sender, object arg)
            {
                Boolean isProcessed = false;
                for (int i = 0; i < Items.Count; i++)
                {
                    if (Items[i].OnEvent(events, sender, arg))
                    {
                        isProcessed = true;
                        break;
                    }

                }

                if (!isProcessed && events == EVENT.GM_MOUSE_CLICKED)
                {
                    BroastCastEvent(EVENT.GM_CURSOR_CONTROL_PET_MOVE, null, null);
                }

                if (events == EVENT.GM_PET_REMOVED || events == EVENT.GM_BULLET_REMOVED)
                {
                    for (int i = 0; i < Items.Count; i++)
                    {
                        if (Items[i] == sender)
                        {
                            Items.RemoveAt(i);
                            break;
                        }
                    }
                }
            }

            public static MyCursor GetCursor()
            {
                return (MyCursor)Items[Global.IndexCursor];
            }

            public static MyForm GetForm()
            {
                return (MyForm)Items[Global.IndexForm];
            }

             public static void SetForm()
            {
                Items[IndexForm] = GetForm().NextForm ;
            }

            public static void ResetItem()
            {
                Items.RemoveAll(isNotMainObject);

                for (int i = 0; i < Items.Count; i++)
                {
                    Type type = Items[i].GetType();
                    if (isForm(type))
                    {
                        IndexForm = i;
                    }
                    if (type == typeof(MyCursor))
                    {
                        IndexCursor = i;
                    }
                }
            }

       /*    public static int GetMoneyOfPlayer()
            {
                for (int i = 0; i < Items.Count; i++)
                {
                    if (Items[i].GetType() == (new MyPlayer()).GetType())
                    {
                        return ((MyPlayer)Items[i]).Money;
                    }
                }
                return 0;
            }*/

            private static Boolean isForm(Type type)
            {
                if (type == typeof(MainMenuScreen) ||
                    type == typeof(ChoseForceScreen))
                {
                    return true;
                }
                return false;
            }

            //Kiem tra cac doi tuong co phai la cursor hoac form hay khong
            private static Boolean isNotMainObject(GameEntity item)
            {
                Type type = item.GetType();
                if (!isForm(type) && type != typeof(MyCursor))
                {
                    return true;
                }
                return false;
            }

            
        
    }
}
